//
//  NetworkOperationManager+Upload.m
//  OneStoreFramework
//
//  Created by huangjiming on 6/12/15.
//  Copyright (c) 2015 OneStore. All rights reserved.
//

#import "UploadFileFromValue.h"
#import "UploadOperationParam.h"
#import "NetworkOperationManager+Upload.h"
#import "NetworkConfig.h"
@interface NetworkOperationParam ()

#pragma mark - 接口调用相关
@property(nonatomic, copy) NSString *requestUrl;                        //请求url
@property(nonatomic, strong) NSDictionary *requestParam;                //参数

@end

@implementation NetworkOperationManager (Upload)

- (NSURLSessionTask *)uploadWithParam:(UploadOperationParam *)aParam {
    if (aParam == nil) {
        return nil;
    }
    
    //超时时间
    self.requestSerializer.timeoutInterval = aParam.timeoutTime;
    
    //dns
    NSString *requestUrl = aParam.requestUrl;
    NSString *currentDomain = [NSURL URLWithString:[NetworkConfig sharedInstance].currentNetworkDomain.networkDomain].host;
    
    NSRange requestUrlRange = NSMakeRange(NSNotFound, 0);
    if (currentDomain.length > 0) {
        requestUrlRange = [requestUrl rangeOfString:currentDomain];
    }
    
    if (requestUrlRange.location != NSNotFound) {
        NSURL *url = [NSURL URLWithString:requestUrl];
        [self.requestSerializer setValue:url.host forHTTPHeaderField:@"Host"];
    } else {
        [self.requestSerializer setValue:nil forHTTPHeaderField:@"Host"];
    }
    
//    [self POST:aParam.requestUrl parameters:aParam.requestParam constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        [self uploadPart:aParam formData:formData];
//    } progress:nil
//    success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        if (aParam.callbackBlock) {
//            aParam.callbackBlock(responseObject, nil);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        if (aParam.callbackBlock) {
//            aParam.callbackBlock(nil, error);
//        }
//    }];
    
    return nil;
}

- (void)uploadPart:(UploadOperationParam *)aParam formData:(id<AFMultipartFormData>)formData {
    NSString *mimeTypeStr = nil;
    if (aParam.uploadType == kUpLoadData) {
        switch (aParam.mimeType) {
            case kPng:
                mimeTypeStr = @"image/png";
                break;
            case kJpg:
                mimeTypeStr = @"image/jpg";
                break;
            default:
                break;
        }
        if (aParam.uploadFileValues.count > 0) {
            for (UploadFileFromValue *fileValue in aParam.uploadFileValues) {
                [formData appendPartWithFileData:fileValue.fileData name:aParam.name fileName:fileValue.fileName mimeType:mimeTypeStr];
            }
        } else {
            [formData appendPartWithFileData:aParam.fileData name:aParam.name fileName:aParam.fileName mimeType:mimeTypeStr];
        }
    } else {
        [formData appendPartWithFileURL:aParam.fileUrl name:aParam.name error:nil];
    }
}

@end
